<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jinzhao
 * Date: 16-8-18
 * Time: 上午11:34
 */
class Address extends MY_Controller{
    public function create()
    {
        $this->load->helper('form');
        
        $data['email'] = $this->session->userdata('email');
        
       $this->load->view('create_server.html', $data); 
    }
    
    public function create_result()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $result = $this->form_validation->run('address');
        if (!$result){
            $this->create();
            return;
        }
        $address = array(
            'name' => $this->input->post('project_name'),
            'ip' => $this->input->post('ip'),
            'port' => $this->input->post('port')
        );
        
        $this->load->model('address_model');
        $result = $this->address_model->create($address);
        if ($result){
            $data = array(
                'title' => '添加后台地址',
                'msg' => '添加成功'
            );
        }else{
            $data = array(
                'title' => '添加后台地址',
                'msg' => '添加失败'
            );
            
        }
        $this->load->view('success.html', $data);
    }
    
    public function delete()
    {

        $id = $this->input->post('id');

        $this->load->model('address_model');
        $result = $this->address_model->delete($id);
        if ($result){
            $data['code'] = 'ok';
        }else{
            $data['code'] = 'error';
        }
        echo json_encode($data);
    }
    
    public function update()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $result = $this->form_validation->run('address');
        if (!$result){
            $data['code'] = 'error';
            $data['msg'] = $this->form_validation->error_string('<p class="text-danger">', '</p>');
            
            $output = json_encode($data);
            echo $output;
            return;
        }
        
        
        $this->load->model('address_model');
        $result = $this->address_model->update();
        if (!$result){
            $data['code'] = 'error';
            $data['msg'] = $this->address_model->display_errors();
        }else {
            $data['code'] = 'ok';
        }
        $output = json_encode($data);
        echo $output;
    }
}